package com.pioneer.admin.controller.book;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.book.domain.BorrowRecord;
import com.pioneer.web.book.service.IBorrowRecordService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 记录
 *
 * @author xx
 */
@RestController
@RequestMapping("/borrow/record")
public class BorrowRecordController extends BaseController {

    @Resource
    private IBorrowRecordService borrowRecordService;

    /**
     * 获取记录列表
     *
     * @param borrowRecord 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('borrow:record:list')")
    @GetMapping("/list")
    public AjaxResult list(BorrowRecord borrowRecord) {
        boolean isPage = startPage();
        List<BorrowRecord> list = borrowRecordService.list(Wrappers.query(borrowRecord));
        return success(isPage, list);
    }


    /**
     * 根据记录id获取详细信息
     *
     * @param borrowRecordId 记录id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('borrow:record:query')")
    @GetMapping(value = "/{borrowRecordId}")
    public AjaxResult getInfo(@PathVariable Long borrowRecordId) {
        return AjaxResult.success(borrowRecordService.getBorrowRecordById(borrowRecordId));
    }

    /**
     * 归还
     *
     * @param borrowRecordIds 书籍ID列表
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('borrow:record:return')")
    @Log(title = "记录", businessType = BusinessType.OTHER)
    @PostMapping("/{borrowRecordIds}")
    public AjaxResult returnBook(@PathVariable Long[] borrowRecordIds) {
        return toAjax(borrowRecordService.returnBook(Arrays.asList(borrowRecordIds)));
    }
}
